package EGS;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.ExecutorService;

import javax.swing.JTable;

import org.dom4j.Document;
import org.dom4j.Element;

import EGS.impl.EGS_execute_service100003Impl;
import EGS.impl.EGS_execute_service100004Impl;
import EGS.impl.EGS_execute_service100006Impl;
import EGS.impl.EGS_execute_service10000Impl;
import EGS.impl.EGS_execute_service10001Impl;
import EGS.impl.EGS_execute_service10003Impl;
import EGS.impl.EGS_execute_service10004Impl;
import EGS.impl.EGS_execute_service10009Impl;
import EGS.impl.EGS_execute_service10010Impl;
import EGS.impl.EGS_execute_service10011Impl;
import EGS.impl.EGS_execute_service10012Impl;
import EGS.impl.EGS_execute_service10013Impl;
import EGS.impl.EGS_execute_service10015Impl;
import EGS.impl.EGS_execute_service10018Impl;
import EGS.impl.EGS_execute_service10021Impl;
import EGS.impl.EGS_execute_service10022Impl;
import EGS.impl.EGS_execute_service10025Impl;
import EGS.impl.EGS_execute_service10101Impl;
import EGS.impl.EGS_execute_service501Impl;
import EGS.impl.EGS_execute_service502Impl;
import EGS.impl.EGS_execute_service520Impl;
import EGS.impl.EGS_execute_service521Impl;
import EGS.impl.EGS_execute_service5506Impl;
import EGS.impl.EGS_execute_service550Impl;
import EGS.impl.EGS_execute_service552Impl;
import EGS.impl.EGS_execute_service555Impl;
import EGS.impl.EGS_execute_service556Impl;
import EGS.impl.EGS_execute_service560Impl;
import EGS.impl.EGS_execute_service561Impl;
import EGS.impl.EGS_execute_service570Impl;
import EGS.impl.EGS_execute_service571Impl;
import EGS.impl.EGS_execute_service572Impl;
import EGS.impl.EGS_execute_service5801Impl;
import EGS.impl.EGS_execute_service580Impl;
import EGS.impl.EGS_execute_service590Impl;
import EGS.impl.EGS_execute_service591Impl;
import EGS.impl.EGS_execute_service595Impl;
import LOGISTICS.c_material;
import LOGISTICS.m_material;
import ORDER.c_order;
import ORDER.m_orderBOM;
import ORDER.orderManagement;
import PRODUCT.PACKAGE.c_package;
import TOOLS.SQLpool;
import TOOLS.SQLtool;
import TOOLS.errCodeManagement;
import TOYS.LASERUPH.laserUPHreporter;
import WORKPLACE.m_SNinfo;
import WORKPLACE.m_panelSonResultState;
import WORKPLACE.m_resultData;
import WORKPLACE.stationClient;

@SuppressWarnings("unused")
public class EGS_execute_service {
	public boolean isUnknowCase = false;
	ExecutorService iExecutorService;
	Document MSG;
	boolean isPanel = false;
	String gName=null;
	stationClient iClient=null;
	SQLtool sql = null;
	public EGS_execute_service(stationClient nClient, Document MSG_in, ExecutorService executorService, 
			SQLtool goldenSql,String threadGname) throws Exception {
		super();
		gName=threadGname;
		sql=goldenSql;
		if(nClient!=null) {
			iClient=nClient;
		}else {
			iClient=null;
		}
		MSG = MSG_in;
		iExecutorService = executorService;
	}
	
	public void close() {
	}

	public Document runCal() {
		String threadName =gName;
		String detailLog = "";
		String msgClass = "";
		try {
			if (MSG == null) {
				return null;
			}
			Element Mroot = MSG.getRootElement();
			msgClass = Mroot.element("header").attribute("messageClass").getValue();
			String transactionID = Mroot.element("header").attribute("transactionID").getValue();
			String iStation, iSN, iLayer, iState;
			int iiLayer, iiState = 0;

			threadName = threadName + "_" + msgClass;
			iStation = Mroot.element("header").element("location").attribute("equipmentName").getValue();
			threadName = threadName + "_" + iStation;
			Thread.currentThread().setName(threadName);

			String endT = "", startT1 = GetTimestamp();
			long diffMS = 0;

			if ((msgClass.equals("501")) || (msgClass.equals("5018"))) {
				MSG=new EGS_execute_service501Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("502")) {
				MSG=new EGS_execute_service502Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("10018")) {// 511 物料信息查询
				MSG=new EGS_execute_service10018Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("520")) {// 520 物料装载_备料_必须有台车元素
				MSG=new EGS_execute_service520Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("521")) {// 521 物料装载_备料_必须有台车元素
				MSG=new EGS_execute_service521Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("540") || msgClass.equals("602")) {// TODO 
				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				eleR.addAttribute("errorCode", "0");
				eleR.addAttribute("errorText", "Valid");
				eleR.addAttribute("actionCode", "0");
				isUnknowCase = true;
			} else if (msgClass.equals("550")|| msgClass.equals("5503")||msgClass.equals("5504")) {
				if(msgClass.equals("5503")) {
					if(!iClient.isPanel) {//5503为了适配德律AOI v3版软件
						throw new Exception("当前版本只能生产拼板工单");
					}
				}
				if(msgClass.equals("5504")) {
					if(iClient.isPanel) {//5504为了适配德律AOI v4版软件
						throw new Exception("当前版本只能生产单板工单");
					}
				}
				MSG=new EGS_execute_service550Impl(iClient,MSG,iExecutorService,sql).runCal();
			}else if (msgClass.equals("552")) {//
				MSG=new EGS_execute_service552Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("555")) {// 上传测量数据 
				MSG=new EGS_execute_service555Impl(iClient,MSG,iExecutorService,sql).runCal();
			}else if(msgClass.equals("556")){//上传测量数据并过站
				MSG=new EGS_execute_service556Impl(iClient,MSG,iExecutorService,sql).runCal();
			}else if(msgClass.equals("560")){//整托盘点胶过站，记录胶水静止时间为测量值,并记录托盘产品关系
				MSG=new EGS_execute_service560Impl(iClient,MSG,iExecutorService,sql).runCal();
			}else if(msgClass.equals("561")){//绑定托盘
				MSG=new EGS_execute_service561Impl(iClient,MSG,iExecutorService,sql).runCal();
			}else if(msgClass.equals("572")){//读取托盘信息
				MSG=new EGS_execute_service572Impl(iClient,MSG,iExecutorService,sql).runCal();
			}else if(msgClass.equals("571")){//清空托盘信息
				MSG=new EGS_execute_service571Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("5506")) {// add by Akite 2020-04-30 edit by Akite 2020-11-17
				MSG=new EGS_execute_service5506Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("570")) {// 整台车上，整台车下
				MSG=new EGS_execute_service570Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("580")) {// 试运行阶段二，用580的时机来切换工单到对应工位。
				MSG=new EGS_execute_service580Impl(iClient,MSG,iExecutorService,sql).runCal();
			}else if (msgClass.equals("5801")) {// 获取工单分类
				MSG=new EGS_execute_service5801Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("590")) {// 物料状态变更（卸载和耗尽）
				MSG=new EGS_execute_service590Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("591")) {// 物料状态变更（卸载和耗尽）
				MSG=new EGS_execute_service591Impl(iClient,MSG,iExecutorService,sql).runCal();
			}else if (msgClass.equals("595")) {// 物料数量同步
				MSG=new EGS_execute_service595Impl(iClient,MSG,iExecutorService,sql).runCal();
			}else if (msgClass.equals("620")) {//TODO 620松下物料无法准确反馈，需要MES主动监控
				String istr=MSG.asXML();
				Mroot.element("body").remove(Mroot.element("body").element("materials"));
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "0");
				eleR.addAttribute("errorText", "Valid");
				eleR.addAttribute("actionCode", "0");
				AKSLogs(6201, ""+istr );
			} else if (msgClass.equals("10000")) {// 申请条码
				MSG=new EGS_execute_service10000Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("10001")) {// 登记条码
				MSG=new EGS_execute_service10001Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("10003")) {// 设备验证
				MSG=new EGS_execute_service10003Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("10004")) {// 人员验证
				MSG=new EGS_execute_service10004Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("10009")) {// 新建包装箱
				MSG=new EGS_execute_service10009Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("10010")) {// 包装箱进出产品
				MSG=new EGS_execute_service10010Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("10011")) {// 获取包装箱信息
				MSG=new EGS_execute_service10011Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("10012")) {// 包装箱状态变更
				MSG=new EGS_execute_service10012Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("10013")) {// 获取产品BIN描述信息
				MSG=new EGS_execute_service10013Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("10015")) {// 设备验证
				MSG=new EGS_execute_service10015Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("100006")) {// 获取BOM
				MSG=new EGS_execute_service100006Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("10101")) {// 核对SN的BIN是否一致
				MSG=new EGS_execute_service10101Impl(iClient,MSG,iExecutorService,sql).runCal();
			}else if (msgClass.equals("10021")) {// TODO 人员信息查询
				MSG=new EGS_execute_service10021Impl(iClient,MSG,iExecutorService,sql).runCal();
			}else if (msgClass.equals("10022")) {// TODO 人员登录
				MSG=new EGS_execute_service10022Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("10025")) {// 获取SN信息
				MSG=new EGS_execute_service10025Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("100003")) {// 发布工单SN
				MSG=new EGS_execute_service100003Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else if (msgClass.equals("100004")) {// 按<拼板>工单获取相关物料信息
				MSG=new EGS_execute_service100004Impl(iClient,MSG,iExecutorService,sql).runCal();
			} else {
				isUnknowCase = true;
				return null;
			}
			endT = GetTimestamp();
			diffMS = getDiffMS(startT1, endT);
//			if(diffMS>500) { //TODO 调试时启用
//				System.out.println(threadName+"【" + msgClass + "】__接口时间" + diffMS);
//			}
			if (diffMS > 3 * 1000) {
				AKSLogs(-105,
						"AKS_transactionID=" + transactionID + "," + System.getProperty("line.separator") + "msgClass="
								+ msgClass + "," + System.getProperty("line.separator") + "totalTime=" + diffMS
								+ "ms" + System.getProperty("line.separator") + detailLog);
			}
			return MSG;
		} catch (Exception e) {
			if(iClient==null) {
				Element Mroot = MSG.getRootElement();
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "1");
				eleR.addAttribute("errorText", "工位信息获取失败");
				eleR.addAttribute("actionCode", "0");
				return MSG;
			}
			String str = errCodeManagement.getErrText(e.getMessage());
			if(str.startsWith("当前版本只能生产")) {
				Element Mroot = MSG.getRootElement();
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "1");
				eleR.addAttribute("errorText", str);
				eleR.addAttribute("actionCode", "0");
				return MSG;
			}
			System.out.println(str);
			return null;
		}

	}


	public void AKSLogs(int iType, String ilogClientNews) {
		SQLtool.AKSLogs(iType, ilogClientNews);
	}
	

	public static String GetTimestamp() {
		// "2017-12-10 15:52:10";
		String timestamp = new String();
		SimpleDateFormat t = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
		timestamp = "" + t.format(new Date());
		return timestamp;
	}

	public static String GetTimestamp(String iFormat) {
		// "2017-12-10 15:52:10";
		if(iFormat == null || iFormat.length()<1 ) {
			iFormat="yyyy-MM-dd HH:mm:ss SSS";
		}
		String timestamp = new String();
		SimpleDateFormat t = new SimpleDateFormat(iFormat);
		timestamp = "" + t.format(new Date());
		return timestamp;
	}

	public static long getDiffMS(String iStartT, String iEndT) {
		SimpleDateFormat t = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
		try {
			Date s = t.parse(iStartT);
			Date e = t.parse(iEndT);
			long diff = e.getTime() - s.getTime();
			if (diff < 0) {
				diff = diff * (-1);
			}
			return diff;
		} catch (ParseException e1) {
			System.out.print(e1.getMessage());
			;
		}
		return -1;
	}
}
